package com.lulin.mall.product.service.impl;

import com.lulin.mall.product.entity.AttrEntity;
import com.lulin.mall.product.service.AttrService;
import com.lulin.mall.product.vo.AttrGroupWithAttrsVo;
import com.lulin.mall.product.vo.SpuItemGroupAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.Query;

import com.lulin.mall.product.dao.AttrGroupDao;
import com.lulin.mall.product.entity.AttrGroupEntity;
import com.lulin.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询列表数据
     * 根据列表编号来查询
     *
     * @param params
     * @param categoryId 如何categoryId为0 就不根据categoryId来查询
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //获取前端的关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            // 查询的条件
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        //如何categoryId = 0, 就不根据categoryId来查询
        if (categoryId == 0) {
            //不根据categoryId来查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
        //不等于O的情况
        //根据类别编号，来查询属性信息
        wrapper.eq("catelog_id", categoryId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    /**
     * 根据三级类目的id，获取对应的属性组，以及相对应的属性信息
     * 1、根据三级类目id，查询对应的所有属性组；
     * 2、根据属性组id，查询对应的属性信息；
     *
     * @param catelogId 类别ids
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrgroupWithAttrs(Long catelogId) {
        //1、根据三级类目id，查询对应的所有属性组；
        List<AttrGroupEntity> attrGroupList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> list = attrGroupList.stream().map((group) -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vo);
            //2、根据属性组id，查询对应的属性信息；
            List<AttrEntity> attrEntityList = attrService.getRelationAttr(group.getAttrGroupId());
            vo.setAttrs(attrEntityList);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 根据 SPU编号和CatalogId类别编号,查询出所有的属性组信息，根据属性组id关联查询到属性id，找到及其属性信息
     *
     * @param spuId     SPU编号s
     * @param catalogId 类别编号
     * @return
     */
    @Override
    public List<SpuItemGroupAttrVo> getAttrgroupWithSpuId(Long spuId, Long catalogId) {
        List<SpuItemGroupAttrVo> groupAttrVo = attrGroupDao.getAttrgroupWithSpuId(spuId, catalogId);
        return groupAttrVo;
    }
}
